package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class ViewOrderServlet extends HttpServlet {

    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    private Order order;
    boolean shippingAddressRequired;
    boolean confirmed;
    List<Order> orderList;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

//        AccountActionBean accountBean = (AccountActionBean) session.getAttribute("accountBean");
        Account account = (Account) session.getAttribute("account");

        //从用户界面跳转来
        if (request.getParameter("comeFromListOrders") != null) {
            OrderService orderService = new OrderService();

            String s = request.getParameter("orderId");
            int orderId = Integer.parseInt(s);
            order = orderService.getOrder(orderId);

            session.setAttribute("order", order);
        }
        //从订单生成界面跳转来
        else if (session.getAttribute("order") != null) {
            order = (Order) session.getAttribute("order");
            OrderService orderService = new OrderService();

            order = orderService.getOrder(order.getOrderId());

            session.setAttribute("order", order);
        }


        if (account.getUsername().equals(order.getUsername())) {
//            return new ForwardResolution(VIEW_ORDER);


            account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + VIEW_ORDER;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(VIEW_ORDER).forward(request, response);
        } else {
            order = new Order();
            session.setAttribute("order", order);

            String message = "You may only view your own orders.";
            session.setAttribute("message", message);

//            return new ForwardResolution(ERROR);

            account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + ERROR;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(ERROR).forward(request, response);
        }
    }
}
